<script>
import { mapState } from 'vuex'
import { zoneGetlistApi, reportViewpdfApi } from '@/requset/api'
import { formConfig, config, column } from './formConfig.js'
import SearchManage from '@/components/basic/SearchManage.vue'
import { requestApi } from '@/requset/api'
import { deepClone } from '@/plugins/clone'
export default {
    extends: SearchManage,
    data() {
        return {
            table_title: '诊断报告',
            url: 'report',
            actions: [
                {
                    label: '批量删除',
                    icon: 'el-icon-delete',
                    click: () => {
                        this.deletes()
                    },
                    if: () => this.operate,
                },
                {
                    label: '添加',
                    icon: 'el-icon-folder-add',
                    click: () => {
                        this.handleAdd()
                    },
                    if: () => this.operate,
                },
            ],
            baseForm: {
                title: null,
                maker: null,
                result: null,
                status: null,
                scope_id_list: [],
                device_id: null,
                filepath: null,
                type: null,
            },
            queryForm: {
                title: null,
                scope_id: null,
                scope_id_list: [],
                start_time: null,
                end_time: null,
                maker: null,
                status: null,
                type: null,
                page: 1,
                size: 20,
            },
            queryFormC: {
                title: null,
                scope_id: null,
                scope_id_list: [],
                type: null,
                start_time: null,
                end_time: null,
                maker: null,
                status: null,
                page: 1,
                size: 20,
            },
        }
    },
    methods: {
        to(data) {
            let list = data.scope_id_list
            data = Object.assign(data, {
                group_id: list.length > 0 ? list[0] : null,
                company_id: list.length > 1 ? list[1] : null,
                factory_id: list.length > 2 ? list[2] : null,
                zone_id: list.length > 3 ? list[3] : null,
            })
            delete data.scope_id_list
            return data
        },
        add(data) {
            data.type = 1
            requestApi('post', this.url + '/add', this.to(deepClone(data))).then((res) => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: '添加成功',
                    })
                    this.getlist()
                    this.$refs.zyform.closeDialog()
                }
            })
        },
        zoneGetlist(zone_id) {
            if (zone_id) {
                zoneGetlistApi(zone_id).then((res) => {
                    if (res.data.code == 0) {
                        this.zone_device_list = res.data.data
                    } else {
                        this.zone_device_list = []
                    }
                })
            }
        },
        handleScopeChange(e) {
            let zone_id = e[e.length - 1]
            this.zoneGetlist(zone_id)
        },
        reportViewpdf(filename) {
            reportViewpdfApi(filename)
        },
    },
    computed: {
        ...mapState('backstage', ['backstageData', 'deviceType']),
        tags() {
            if (this.pagination.total !== undefined) {
                return [{ title: '总数', num: this.pagination.total }, null, { title: '编辑中', num: this.stat.editing }, { title: '已完成', num: this.stat.completed }]
            } else return []
        },
    },
    created() {
        this.searchBtn()
        formConfig[1].options = this.backstageData
        this.formConfig = formConfig
        config[4].options = this.backstageData
        config[4].onChange = (config, form) => {
            form.device_id = null
            if (form.scope_id_list[form.scope_id_list.length - 1] && form.scope_id_list)
                zoneGetlistApi(form.scope_id_list[form.scope_id_list.length - 1]).then((res) => {
                    if (res.data.code == 0) {
                        config[5].options = res.data.data.map((item) => Object.assign(item, { label: item.device_name, value: item.device_id }))
                    } else {
                        config[5].options = []
                    }
                })
        }
        this.dialog.config = config
        column[column.length - 1].buttons = [
            {
                text: '删除',
                click: (val) => this.delete(val),
                icon: 'el-icon-delete',
            },
            {
                text: '编辑',
                click: (val) => {
                    this.handleEdit(val)
                },
                icon: 'el-icon-edit',
            },
        ]
        column[column.length - 1].if = this.operate
        this.tableColumn = column
    },
}
</script>
